<?php
require_once 'mysql.php';

class location_info extends MySQL
{
    # 连接Mysql数据库
    function connect()
    {
        $conn = parent::connect();
        return $conn;
    }

    #覆写父类方法
    public function get_userId($key)
    {
        $ID = parent::get_userId($key);
        return $ID;
    }

    #查询多状态信息
    public function sum_info($tableID)
    {
        $conn = $this->connect();
        $table = $tableID . '_location';
        $risk_table = $tableID . '_risk_district';
        // 执行查询的代码
        $sql = "SELECT COUNT(*) as sum ,(SELECT Risk FROM $table ORDER BY date DESC LIMIT 1) as risk,(SELECT date FROM $table ORDER BY date DESC LIMIT 1) as date ,(SELECT COUNT(*) as today_sum FROM $table WHERE DATE(date) = CURDATE()) as today, (SELECT count(*) FROM $table WHERE Risk>0.9) as warn,(SELECT COUNT(*) FROM $risk_table WHERE is_true=1) as warn_distance FROM $table";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $sum = $row['sum'];
                $today = $row['today'];
                $warn = $row['warn'];
                $warn_distance = $row['warn_distance'];
                $date = $row['date'];
                $Risk = $row['risk'];
                //组合键值对
                $detail = array(
                    'date' => $date,
                    'risk' => $Risk,
                    'warn_distance' => $warn_distance,
                    'warn' => $warn,
                    'today' => $today,
                    'sum' => $sum
                );
            }
            echo json_encode($detail, JSON_UNESCAPED_UNICODE) . "<br>";
        } else { //无记录时自动设置中心区域，应该为注册所在城市地
            $data['KEY'] = "False";
        }
        $conn->close();
    }

    #查询最近10条位置
    public function location_info($tableID,$num)
    {
        $conn = $this->connect();
        $table = $tableID . '_location';
        // 执行查询的代码
        $sql = "SELECT * FROM $table ORDER BY date DESC LIMIT $num;";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            $index = 0;
            while ($row = $result->fetch_assoc()) {
                $ID = $row['ID'];
                $Longitude = $row['Longitude'];
                $Latitude = $row['Latitude'];
                $elevation = $row['elevation'];
                $date = $row['date'];
                $Risk = $row['Risk'];
                //组合键值对
                $detail = array(
                    '经度' => $Longitude,
                    '纬度' => $Latitude,
                    '高程' => $elevation,
                    '时间' => $date,
                    '风险' => $Risk
                );
                $data = array(
                    'Index' => $index++,
                    'ID' => $ID,
                    'detail' => $detail
                );
                $results[] = $data; // 将每次迭代的结果添加到数组中
            }
            echo json_encode($results, JSON_UNESCAPED_UNICODE) . "<br>";
        } else { //无记录时自动设置中心区域，应该为注册所在城市地
            $data['KEY'] = "False";
        }
        $conn->close();
    }
}
// 获取请求中的key
$db = new location_info();
if (isset($_GET['key'])) {
    //获取key
    $ID = $db->get_userId($_GET['key']); //如果key存在且正确，返回用户ID，否则会返回false
    if ($ID != false) {
        //定义相关数组
        $data = array();
        $detail = array();
        if (isset($_GET['sum'])) {
            //查询多状态信息
            $db->sum_info($ID);
        //传递查询条数
        } elseif(isset($_GET['num']) && $_GET['num']!=null && $_GET['num']!='' && $_GET['num']!=0) {
            $db->location_info($ID,$_GET['num']);
        }else{
            //默认查询10条
            $db->location_info($ID,10);
        }
    } else {
        $data['UKEY'] = "False" . $ID;
        echo json_encode($data) . "<br>";
    }
} else {
    $data = array();
    $data['UKEY'] = "NULL" ;
    echo json_encode($data) . "<br>";
}
